package megamek.common.weapons;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.Vector;

import megamek.common.BipedMech;
import megamek.common.Board;
import megamek.common.Coords;
import megamek.common.Entity;
import megamek.common.Game;
import megamek.common.Hex;
import megamek.common.IGame;
import megamek.common.Mech;
import megamek.common.MiscType;
import megamek.common.Mounted;
import megamek.common.Player;
import megamek.common.ToHitData;
import megamek.common.WeaponType;
import megamek.common.actions.WeaponAttackAction;
import megamek.server.Server;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

// TODO: Auto-generated Javadoc
/**
 * The Class BombastLaserWeaponHandlerTest.
 */
public class BombastLaserWeaponHandlerTest
{

	/** The game. */
	static IGame game = new Game();

	/** The server. */
	static Server server;

	/** The to hit data. */
	static ToHitData toHitData = new ToHitData(ToHitData.HIT_ABOVE, "hit above");
	/** The attacker. */
	static Player attacker = new Player(1, "attacker");
	/** The target. */
	static Player target = new Player(2, "target");
	/** The target ent. */
	static Mech attackerEnt, targetEnt;

	/** The entities. */
	static Vector<Entity> entities;

	/** The action. */
	static WeaponAttackAction action;

	/** The bombast laser. */
	static BombastLaserWeaponHandler bombastLaser;

	/** The weapon. */
	static WeaponType weapon = new WeaponType();

	/**
	 * Sets the up before class.
	 * 
	 * @throws Exception
	 *             the exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{
		try
		{
			// create a new server
			server = new Server("password", 111);
		}
		catch (IOException e)
		{
			System.err.println("Error creating the server - message: " + e.getMessage());
		}

		// create the board and hexs to fill the board
		game.setBoard(new Board(3, 3));
		for (int i = 0; i < game.getBoard().getWidth(); i++)
		{
			for (int j = 0; j < game.getBoard().getHeight(); j++)
			{
				game.getBoard().setHex(new Coords(i, j), new Hex(0));
			}
		}

		game.addPlayer(attacker.getId(), attacker);
		game.addPlayer(target.getId(), target);

		// add the entities to the game
		attackerEnt = new BipedMech();
		attackerEnt.setOwner(attacker);
		attackerEnt.setId(15);
		attackerEnt.setPosition(new Coords(0, 0));
		attackerEnt.setWeight(200);

		targetEnt = new BipedMech();
		targetEnt.setOwner(target);
		targetEnt.setId(20);
		targetEnt.setPosition(new Coords(1, 0));

		entities = new Vector<Entity>();
		entities.add(attackerEnt);
		entities.add(targetEnt);
		game.setEntitiesVector(entities);

		// CLBAAPGaussRifle weapon = new CLBAAPGaussRifle();
		// weapon
		weapon.setDamage(25);
		Mounted mounted = new Mounted(attackerEnt, weapon);
		attackerEnt.addEquipment(mounted, 0, false);
		// attackerEnt.addEquipment(, 1, false);
		// ;

		action = new WeaponAttackAction(attackerEnt.getId(), targetEnt.getId(), 0);
		bombastLaser = new BombastLaserWeaponHandler(toHitData, action, game, server);
	}

	/**
	 * Tear down after class.
	 * 
	 * @throws Exception
	 *             the exception
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception
	{
	}

	/**
	 * Sets the up.
	 * 
	 * @throws Exception
	 *             the exception
	 */
	@Before
	public void setUp() throws Exception
	{
	}

	/**
	 * Tear down.
	 * 
	 * @throws Exception
	 *             the exception
	 */
	@After
	public void tearDown() throws Exception
	{
	}

	/**
	 * Test calc damage per hit.
	 */
	@Test
	public void testCalcDamagePerHit()
	{
		int result = bombastLaser.calcDamagePerHit();
		assertEquals(weapon.getDamage(), result);
	}

	/**
	 * Test add heat.
	 */
	@Test
	public void testAddHeat()
	{
		bombastLaser.addHeat();
		assertEquals(attackerEnt.heat, game.getEntity(bombastLaser.getAttackerId()).heat);
	}
}
